package org.jeecg.modules.oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.jeecg.modules.oa.entity.ButtonConditionNew;
import org.jeecg.modules.oa.entity.NodeButtonNew;
import org.jeecg.modules.oa.mapper.ButtonConditionNewMapper;
import org.jeecg.modules.oa.mapper.NodeButtonNewMapper;
import org.jeecg.modules.oa.service.INodeButtonNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 节点
 * @Author: jeecg-boot
 * @Date:   2019-12-17
 * @Version: V1.0
 */
@Service
public class NodeButtonNewServiceImpl extends ServiceImpl<NodeButtonNewMapper, NodeButtonNew> implements INodeButtonNewService {

	@Autowired
	private NodeButtonNewMapper nodeButtonNewMapper;
	@Autowired
	private ButtonConditionNewMapper buttonConditionNewMapper;
	
	@Override
	@Transactional
	public void saveMain(NodeButtonNew nodeButtonNew, List<ButtonConditionNew> buttonConditionNewList) {
		nodeButtonNewMapper.insert(nodeButtonNew);
		for(ButtonConditionNew entity:buttonConditionNewList) {
			//外键设置
			entity.setButtonId(nodeButtonNew.getId());
			buttonConditionNewMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(NodeButtonNew nodeButtonNew,List<ButtonConditionNew> buttonConditionNewList) {
		nodeButtonNewMapper.updateById(nodeButtonNew);
		
		//1.先删除子表数据
		buttonConditionNewMapper.deleteByMainId(nodeButtonNew.getId());
		
		//2.子表数据重新插入
		for(ButtonConditionNew entity:buttonConditionNewList) {
			//外键设置
			entity.setButtonId(nodeButtonNew.getId());
			buttonConditionNewMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		buttonConditionNewMapper.deleteByMainId(id);
		nodeButtonNewMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			buttonConditionNewMapper.deleteByMainId(id.toString());
			nodeButtonNewMapper.deleteById(id);
		}
	}
	
}
